package com.jason.www.state_pattern.demo2.context;


import lombok.Data;
import com.jason.www.state_pattern.demo2.state.ATMNoCashState;
import com.jason.www.state_pattern.demo2.state.ATMNoServiceState;
import com.jason.www.state_pattern.demo2.state.ATMReadyState;
import com.jason.www.state_pattern.demo2.state.IATMState;

@Data
public class ATMContext {
    /*所有状态*/
    private IATMState readyState;
    private IATMState noCashState;
    private IATMState noServiceState;

    private IATMState currState;

    private String password;
    /**
     * 余额
     */
    private Integer balance;
    /**
     *  提现金额
     */
    private Integer takeMoney;
    /**
     * ATM总金额
     */
    private Integer totalMoney;

    public ATMContext(int totalAmount, int balance, int amount, String pwd) throws Exception{
        //初始化所有状态
        readyState = new ATMReadyState(this);
        noCashState = new ATMNoCashState(this);
        noServiceState = new ATMNoServiceState(this);

        if(totalAmount > 0){
            currState = readyState;
        }
        else if(totalAmount == 0){
            currState = noCashState;
        }
        else{
            throw new Exception();
        }

        //初始化测试数据
        this.totalMoney = totalAmount;
        this.balance = balance;
        this.takeMoney = amount;
        this.password = pwd;
    }

    /*把具体行为委托给状态对象*/
    /**
     * 插卡
     */
    public void insertCard(){
        currState.insertCard();
    }

    /**
     * 提交密码
     */
    public void submitPwd(){
        currState.submitPwd();
    }

    /**
     * 取款
     */
    public void getCash(){
        currState.getCash();
    }

    /**
     * 查询余额
     */
    public void queryBalance(){
        currState.queryBalance();
    }

    /**
     * 取卡
     */
    public void ejectCard(){
        currState.ejectCard();
    }

    @Override
    public String toString(){
        return "现钞总数￥" + totalMoney;
    }

}
